/**
 * This is a hack to make inputEx fields "wirable"
 *
 * We save the class inputEx.Field into inputEx.BaseField, 
 * then we re-create inputEx.Field that inherits from inputEx.BaseField
 * We can then add the properties to make every input wirables.
 * 
 */

// Copy inputEx.Field to inputEx.BaseField to override inputEx.Field
inputEx.BaseField = inputEx.Field;

inputEx.Field = function(options) {
   
   inputEx.Field.superclass.constructor.call(this,options);
  
   this.addTerminal(); 
};

/**
 * Register this class as "string" type
 */
inputEx.registerType("string", inputEx.Field);


YAHOO.extend(inputEx.Field, inputEx.BaseField, {
   
   addTerminal: function() {
      
      var wrapper = inputEx.cn('div', {className: 'dfly-InputTerminal-Wrapper'});
      this.divEl.insertBefore(wrapper, this.divEl.firstChild);

      if(this.options.wirable) {
         this.terminal = new WireIt.Terminal(wrapper, {
            name: this.options.name, 
            direction: [-1,0],
            fakeDirection: [0, 1],
            ddConfig: {
               type: "input",
               allowedTypes: ["output"]
            },
            nMaxWires: 1 }, this.options.container);
         
         // Dfly name for this terminal
         this.terminal.dflyName = "input_"+this.options.name;
         
         // Reference to the container
         this.options.container.terminals.push(this.terminal);

         // Register the events
         this.terminal.eventAddWire.subscribe(this.onAddWire, this, true);
         this.terminal.eventRemoveWire.subscribe(this.onRemoveWire, this, true);
      }

   },
   
   onAddWire: function(e, params) {
   
      this.options.container.onAddWire(e,params);
   
      this.disable();
      this.el.value = "[wired]";
   },
   
   /**
    * Remove the input wired state
    */
   onRemoveWire: function(e, params) { 
      this.options.container.onRemoveWire(e,params);
      
      this.enable();
      this.el.value = "";
   }
   
});


